﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using FSS.Core.Util;

namespace SalesManagement.Controls.List
{
    public partial class ProductForm : BaseControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindingCategory();
                BindingUnit(); ;
                BindingWarehouse();
            }
            this.WindowEditorPrice.SaveClick += new EventHandler(WindowEditorPrice_SaveClick);
            this.WindowEditorMeasure.SaveClick += new EventHandler(WindowEditorMeasure_SaveClick);
            this.WindowEditorCategory.SaveClick += new FormEventHandler(WindowEditorCategory_SaveClick);
        }

        #region Control
        void WindowEditorCategory_SaveClick(object sender, FormEventArgs e)
        {
            string id = e.ExtraParam;
            BindingCategory();
        }
        void WindowEditorPrice_SaveClick(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtProductId.Text);
            BindingPrirebyProductId(id);
        }
        void WindowEditorMeasure_SaveClick(object sender, EventArgs e)
        {
            int id = Int32.Parse(txtProductId.Text);
            BindingMeasurebyProductId(id);
        }
        public void btnAddUnit_Click(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(txtProductId.Text);
            this.WindowEditorMeasure.Show(id);
        }
        public void btnAddPrice_Click(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(txtProductId.Text);
            this.WindowEditorPrice.Show(id);
        }
        protected void btnAddCategory_Click(object sender, DirectEventArgs e)
        {
            this.WindowEditorCategory.ShowAndReset();
        }
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        #endregion
        #region Command
        protected void grdMeasure_OnCommand(object sender, DirectEventArgs e)
        {
            int productid = Util.Obj2Int(e.ExtraParams["ProductId"]);
            int baseunit = Util.Obj2Int(e.ExtraParams["BasicUnit"]);
            int ratiounit = Util.Obj2Int(e.ExtraParams["RatioUnit"]);
            string command = e.ExtraParams["command"];
            if (Constants.Edit.Equals(command))
            {
                this.WindowEditorMeasure.ShowEdit(productid, baseunit, ratiounit);
            }
            else if (Constants.Delete.Equals(command))
            {
                MeasureEntity measureE = new MeasureEntity();
                MeasureService measureS = MeasureService.GetInstance();
                measureE.ProductId = productid;
                measureE.BasicUnit = baseunit;
                measureE.RatioUnit = ratiounit;
                measureS.Delete(measureE);

            }
            BindingMeasurebyProductId(productid);
        }
        protected void grdPrice_OnCommand(object sender, DirectEventArgs e)
        {
            
            int productpriceid = Format.ConvertToInt(e.ExtraParams["ProductPriceId"]);
            int productid = Format.ConvertToInt(e.ExtraParams["ProductId"]);
            string command = e.ExtraParams["command"];
            if (Constants.Edit.Equals(command))
            {
                this.WindowEditorPrice.ShowEdit(productpriceid);
            }
            BindingPrirebyProductId(productid);
        }
        #endregion
        #region Excute

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            Insert();
            setFormEnable();
            //this.winDetails.Hide();
            OnSaveClick();

        }
        private void setFormDisnable()
        {
            FormChangeUnit.Disabled = true;
            FormProductPrice.Disabled = true;
        }
        private void setFormEnable()
        {
            FormChangeUnit.Disabled = false;
            FormProductPrice.Disabled = false;
        }
        public void Show()
        {
            txtProductId.Text = "";
            this.PanelWindow.Reset();
            tabProductForm.ActiveTabIndex = 0;
            setFormDisnable();
            BindingLoad();
            this.winDetails.Show();
        }
        public void Show(int ID)
        {
            setFormEnable();
            tabProductForm.ActiveTabIndex = 0;
            txtProductId.Text = ID.ToString();
            BindingMeasurebyProductId(ID);
            BindingPrirebyProductId(ID);
            BindingLoad();
            ProductEntity productE = new ProductEntity();
            ProductService productS = ProductService.GetInstance();
            productE.ProductId = ID;
            productE = productS.LoadByPrimaryKey(productE);
            if (!productE.Equals(null))
            {
                txtProductCode.Text = productE.ProductCode;
                txtProductName.Text = productE.ProductName;
                cboCategoryId.SelectedItem.Value = productE.CategoryId.ToString();
                cboInputUnit.SelectedItem.Value = productE.InputUnit.ToString();
                cboOutUnit.SelectedItem.Value = productE.OutUnit.ToString();
                cboStockUnit.SelectedItem.Value = productE.StockUnit.ToString();
                if (productE.IsMaterial.Equals(true)) chkIsMaterial.Checked = true;
                else chkIsMaterial.Checked = false;
                if (productE.IsProduct.Equals(true)) chkIsProduct.Checked = true;
                else chkIsProduct.Checked = false;
                if (productE.IsRegular.Equals(true)) chkIsRegular.Checked = true;
                else chkIsRegular.Checked = false;
                if (productE.IsWarranty.Equals(true)) chkIsWarranty.Checked = true;
                else chkIsWarranty.Checked = false;
                txtOutputPrice.Text = (Convert.ToDouble(productE.OutputPrice)).ToString();
                txtInputPrice.Text = (Convert.ToDouble(productE.InputPrice)).ToString();
                txtInputDiscount.Text = (Convert.ToDouble(productE.InputDiscount)).ToString();
                txtOutputDiscount.Text = (Convert.ToDouble(productE.OutpuDiscount)).ToString();
                txtOutputVAT.Text = (Convert.ToDouble(productE.OutputVAT)).ToString();
                txtInputVAT.Text = (Convert.ToDouble(productE.InputVAT)).ToString();
                txtMaxStock.Text = (Convert.ToDouble(productE.MaxStock)).ToString();
                txtMinStock.Text = (Convert.ToDouble(productE.MinStock)).ToString();
                cboWareHouse.SelectedItem.Value = productE.WarehouseId.ToString();
                txtDescription.Text = productE.Description;

                this.winDetails.Show();
            }
            else
                return;
        }
        protected void Insert()
        {
            ProductEntity productE = new ProductEntity();
            ProductService productS = ProductService.GetInstance();
            productE.ProductCode = txtProductCode.Text;
            productE.ProductName = txtProductName.Text;
            productE.CategoryId = Int32.Parse(cboCategoryId.SelectedItem.Value);
            productE.OutUnit = Int32.Parse(cboOutUnit.SelectedItem.Value);
            productE.InputUnit = Int32.Parse(cboInputUnit.SelectedItem.Value);
            productE.StockUnit = Int32.Parse(cboStockUnit.SelectedItem.Value);
            if (chkIsMaterial.Checked.Equals(true)) productE.IsMaterial = true;
            else productE.IsMaterial = false;
            if (chkIsProduct.Checked.Equals(true)) productE.IsProduct = true;
            else productE.IsProduct = false;
            productE.OutputPrice = Decimal.Parse(txtOutputPrice.Text);
            productE.InputPrice = Decimal.Parse(txtInputPrice.Text);
            productE.InputVAT = Decimal.Parse(txtInputVAT.Text);
            productE.OutputVAT = Decimal.Parse(txtOutputVAT.Text);
            productE.MinStock = Decimal.Parse(txtMinStock.Text);
            productE.MaxStock = Decimal.Parse(txtMaxStock.Text);
            productE.InputDiscount = Decimal.Parse(txtInputDiscount.Text);
            productE.OutpuDiscount = Decimal.Parse(txtOutputDiscount.Text);
            if (chkIsRegular.Checked.Equals(true)) productE.IsRegular = true;
            else productE.IsRegular = false;
            if (chkIsWarranty.Checked.Equals(true)) productE.IsWarranty = true;
            else productE.IsWarranty = false;
            productE.Description = txtDescription.Text;
            productE.WarehouseId = Int32.Parse(cboWareHouse.SelectedItem.Value);
            productE.CreatedDate = DateTime.Now;
            productE.CreatedBy = FSSContext.Current.User.EmployeeId;
            productE.UpdatedDate = DateTime.Now;
            productE.UpdatedBy = FSSContext.Current.User.EmployeeId;
            productE.IsActive = true;
            if (txtProductId.Text.Equals(""))
            {
                //productS.Insert(productE);
                txtProductId.Text = productS.InsertUpdate(productE).ToString();
                int id = Int32.Parse(txtProductId.Text);
                BindingMeasurebyProductId(id);
                BindingPrirebyProductId(id);
            }
            else
            {
                productE.ProductId = Int32.Parse(txtProductId.Text);
                productS.Update(productE);
            }
        }
        //private bool CheckInputData()
        //{
        //    string msg = "";
        //    if (txtProductCode.Text.Equals(""))
        //        msg += "<br/>Mã hàng";
        //    if (txtProductName.Text.Equals(""))
        //        msg += "<br/>Tên hàng";
        //    if (txtOutputPrice.Text.Equals(""))
        //        msg += "<br/>Giá bán";
        //    if (txtInputVAT.Text.Equals(""))
        //        msg += "<br/>Thuế nhập";
        //    if (txtOutputVAT.Text.Equals(""))
        //        msg += "<br/>Thuế xuấ";
        //    if (txtMinStock.Text.Equals(""))
        //        msg += "<br/>Tồn kho tối thiểu";
        //    if (txtMaxStock.Text.Equals(""))
        //        msg += "<br/>Tồn kho tối đa";
        //    if (txtInputPrice.Text.Equals(""))
        //        msg += "<br/>Giá mua";
        //    if (txtInputDiscount.Text.Equals(""))
        //        msg += "<br/>Chiết khấu nhập";
        //    if (txtOutputDiscount.Text.Equals(""))
        //        msg += "<br/>Chiết khấu xuất";
        //    if (!msg.Equals(""))
        //    {
        //        X.Msg.Alert("Lỗi nhập", "Nội dung cần nhập:" + msg + "<br>Vui lòng nhập đầy đủ thông tin").Show();
        //        return false;
        //    }
        //    return true;
        //}
        #endregion
        #region binding

        private void BindingLoad()
        {
            BindingCategory();
            BindingUnit();
            BindingWarehouse();
        }
        /// <summary>
        /// Load bảng đơn vị
        /// </summary>
        private void BindingUnit()
        {
            UnitEntity unitE = new UnitEntity();
            UnitService unitS = UnitService.GetInstance();
            List<UnitEntity> listUnit = (List<UnitEntity>)unitS.LoadAll();
            if (!listUnit.Equals(null))
            {
                storeUnit.DataSource = listUnit;
                storeUnit.DataBind();
                cboInputUnit.SelectedIndex = 0;
                cboOutUnit.SelectedIndex = 0;
                cboStockUnit.SelectedIndex = 0;
            }
        }
        private void BindingPrirebyProductId(int Id)
        {
            ProductPriceEntity priceE = new ProductPriceEntity();
            ProductPriceService priceS = ProductPriceService.GetInstance();
            priceE.ProductId = Id;
            List<ProductPriceEntity> listPrice = (List<ProductPriceEntity>)priceS.LoadByProductId(priceE);
            if (!listPrice.Equals(null))
            {
                storeProductPrice.DataSource = listPrice;
                storeProductPrice.DataBind();
            }
        }
        private void BindingMeasurebyProductId(int Id)
        {
            MeasureEntity measureE = new MeasureEntity();
            MeasureService measureS = MeasureService.GetInstance();
            measureE.ProductId = Id;
            List<MeasureEntity> listMeasure = (List<MeasureEntity>)measureS.LoadByProductId(measureE);
            if (!listMeasure.Equals(null))
            {
                storeMeasure.DataSource = listMeasure;
                storeMeasure.DataBind();
            }
        }
        /// <summary>
        /// load bảng nhóm hàng
        /// </summary>
        private void BindingCategory()
        {
            CategoryEntity categoryE = new CategoryEntity();
            CategoryService categoryS = CategoryService.GetInstance();
            List<CategoryEntity> listCategory = (List<CategoryEntity>)categoryS.LoadAll();
            if (!listCategory.Equals(null))
            {
                storeCategory.DataSource = listCategory;
                storeCategory.DataBind();
                cboCategoryId.SelectedIndex = 0;
            }
        }
        private void BindingWarehouse()
        {
            WareHouseEntity warehouseE = new WareHouseEntity();
            WareHouseService warehouseS = WareHouseService.GetInstance();
            List<WareHouseEntity> listWarehouse = (List<WareHouseEntity>)warehouseS.LoadAll();
            if (!listWarehouse.Equals(null))
            {
                storeWarehouse.DataSource = listWarehouse;
                storeWarehouse.DataBind();
                cboWareHouse.SelectedIndex = 0;
            }
        }
        #endregion
    }
}